﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;

namespace Joiner
{
    class Program
    {
        static void Main(string[] args)
        {
            bool size = false;

            Bitmap result = null;
            for (int i = 0; i < args.Length; i++)
            {
                int[] info = getFileInfo(args[i]);
                if (!size)
                {
                    result = new Bitmap(info[1], info[2]);
                    size = true;
                    Console.WriteLine("Bitmap initialized " + result.Width + ", " + result.Height);
                }

                Bitmap bmp = new Bitmap("section" + i + ".png");

                for (int k = 0; k < bmp.Width; k++)
                    for (int j = 0; j < bmp.Height; j++)
                    {
                        result.SetPixel(k + info[3], j + info[4], bmp.GetPixel(k, j));
                    }

            }

            MemoryStream ms = new MemoryStream();
            result.Save(ms, ImageFormat.Png);
            FileStream fs = new FileStream("combined.png", FileMode.Create);
            ms.WriteTo(fs);
            ms.Close();
            fs.Close();

        }

        static int[] getFileInfo(String file)
        {
            int[] info = new int[7];

            FileStream config = new FileStream(file, FileMode.Open, FileAccess.Read);
            StreamReader cdr = new StreamReader(config);
            info[0] = Int32.Parse(cdr.ReadLine().Split(' ')[2]);
            info[1] = Int32.Parse(cdr.ReadLine().Split(' ')[2]);
            info[2] = Int32.Parse(cdr.ReadLine().Split(' ')[2]);
            info[3] = Int32.Parse(cdr.ReadLine().Split(' ')[2]);
            info[4] = Int32.Parse(cdr.ReadLine().Split(' ')[2]);
            info[5] = Int32.Parse(cdr.ReadLine().Split(' ')[2]);
            info[6] = Int32.Parse(cdr.ReadLine().Split(' ')[2]);
            cdr.Close();
            return info;
        }
    }
}
